package com.whmnx.chitchat.adapter.holder;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.openqq.protocol.imsdk.msg;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.bean.ChatDetailsBean;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.constants.MessageInfo;
import com.whmnx.chitchat.fragment.ChatFragment;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.main.MainActivity;
import com.whmnx.chitchat.ui.chat.AddMoreActivity;
import com.whmnx.chitchat.ui.chat.ChatActivity;
import com.whmnx.chitchat.ui.chat.FriendInfoActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author Administrator
 */
public abstract class MessageContentHolder2 extends MessageEmptyHolder2 {

    public CircleImageView leftUserIcon;
    public CircleImageView rightUserIcon;
    public TextView usernameText;
    public LinearLayout msgContentLinear;
    public ProgressBar sendingProgress;
    public ImageView statusImage;
    public TextView isReadText;
    public TextView unreadAudioText;

    public MessageContentHolder2(View itemView) {
        super(itemView);
        rootView = itemView;

        leftUserIcon = itemView.findViewById(R.id.left_user_icon_view);
        rightUserIcon = itemView.findViewById(R.id.right_user_icon_view);
        usernameText = itemView.findViewById(com.tencent.qcloud.tim.uikit.R.id.user_name_tv);
        statusImage = itemView.findViewById(com.tencent.qcloud.tim.uikit.R.id.message_status_iv);
        msgContentLinear = itemView.findViewById(com.tencent.qcloud.tim.uikit.R.id.msg_content_ll);
        sendingProgress = itemView.findViewById(com.tencent.qcloud.tim.uikit.R.id.message_sending_pb);
        isReadText = itemView.findViewById(com.tencent.qcloud.tim.uikit.R.id.is_read_tv);
        unreadAudioText = itemView.findViewById(com.tencent.qcloud.tim.uikit.R.id.audio_unread);
    }

    @Override
    public void layoutViews(final ChatDetailsBean msg, final int position) {
        super.layoutViews(msg, position);

        //// 头像设置
        if (msg.isSelf()) {
            leftUserIcon.setVisibility(View.GONE);
            rightUserIcon.setVisibility(View.VISIBLE);
            ImageUtil.setImage(rightUserIcon,SPUtil.getString(Constants.HEAD_IMG));
         //   rightUserIcon.setDefaultImageResId(R.drawable.default_head);
        } else {
            leftUserIcon.setVisibility(View.VISIBLE);
            rightUserIcon.setVisibility(View.GONE);
            //单聊
            if (!TextUtils.isEmpty(msg.getSendUserHeadImg())){
                ImageUtil.setImage(leftUserIcon, msg.getSendUserHeadImg());
            }else {
                if (!TextUtils.isEmpty(msg.getToUserHeadImg())){
                    ImageUtil.setImage(leftUserIcon, msg.getToUserHeadImg());
                }else {
                    Map<String, Object> params = new HashMap<>();
                    params.put("userId", msg.getChatRecord_SendUserID());
                    HttpClient.get(MainActivity.mContext, Constants.GETUSERINFO, params, new CallBack<UserBean>() {
                        @Override
                        public void onSuccess(UserBean loginBean) {
                            ImageUtil.setImage(leftUserIcon, loginBean.getHeadImg());
                            SPUtil.put(Constants.IMG,loginBean.getHeadImg());
                        }
                    });
                }
               // ImageUtil.setImage(leftUserIcon, msg.getToUserHeadImg());
               // String img = SPUtil.getString(Constants.IMG);
            }
        }
      /*  if (properties.getAvatar() != 0) {
            leftUserIcon.setDefaultImageResId(properties.getAvatar());
            rightUserIcon.setDefaultImageResId(properties.getAvatar());
        } else {
            leftUserIcon.setDefaultImageResId(R.drawable.default_head);
            rightUserIcon.setDefaultImageResId(R.drawable.default_head);
        }
        if (properties.getAvatarRadius() != 0) {
            leftUserIcon.setRadius(properties.getAvatarRadius());
            rightUserIcon.setRadius(properties.getAvatarRadius());
        } else {
            leftUserIcon.setRadius(5);
            rightUserIcon.setRadius(5);
        }
        if (properties.getAvatarSize() != null && properties.getAvatarSize().length == 2) {
            ViewGroup.LayoutParams params = leftUserIcon.getLayoutParams();
            params.width = properties.getAvatarSize()[0];
            params.height = properties.getAvatarSize()[1];
            leftUserIcon.setLayoutParams(params);

            params = rightUserIcon.getLayoutParams();
            params.width = properties.getAvatarSize()[0];
            params.height = properties.getAvatarSize()[1];
            rightUserIcon.setLayoutParams(params);
        }*/
      //  leftUserIcon.invokeInformation(msg);
    //    rightUserIcon.invokeInformation(msg);

        //// 用户昵称设置
        if (msg.isSelf()) { // 默认不显示自己的昵称
            if (properties.getRightNameVisibility() == 0) {
                usernameText.setVisibility(View.GONE);
            } else {
                usernameText.setVisibility(properties.getRightNameVisibility());
            }
        } else {

        }
        if (properties.getNameFontColor() != 0) {
            usernameText.setTextColor(properties.getNameFontColor());
        }
        if (properties.getNameFontSize() != 0) {
            usernameText.setTextSize(properties.getNameFontSize());
        }
        // 聊天界面设置头像和昵称
       /* TIMUserProfile profile = TIMFriendshipManager.getInstance().queryUserProfile(msg.getFromUser());
        if (profile == null) {
            usernameText.setText(msg.getFromUser());
        } else {

        }*/
        TIMUserProfile selfInfo = TIMFriendshipManager.getInstance().queryUserProfile(TIMManager.getInstance().getLoginUser());
        if (selfInfo != null && msg.isSelf()) {
            if (!TextUtils.isEmpty(selfInfo.getFaceUrl())) {
                List<Object> urllist = new ArrayList<>();
                urllist.add(selfInfo.getFaceUrl());
             //   rightUserIcon.setIconUrls(urllist);
                urllist.clear();
            }
        }

        if (msg.getStatus() == MessageInfo.MSG_STATUS_SENDING) { //消息发送状态
            sendingProgress.setVisibility(View.VISIBLE);
        } else {
            sendingProgress.setVisibility(View.GONE);
        }

        //// 聊天气泡设置
        if (msg.isSelf()) {
            if (properties.getRightBubble() != null && properties.getRightBubble().getConstantState() != null) {
                msgContentFrame.setBackground(properties.getRightBubble().getConstantState().newDrawable()); //chat_bubble_myself
            } else {
                msgContentFrame.setBackgroundResource(com.whmnx.chitchat.R.drawable.chat_bubble_myself);
            }
        } else {
            if (properties.getLeftBubble() != null && properties.getLeftBubble().getConstantState() != null) {
                msgContentFrame.setBackground(properties.getLeftBubble().getConstantState().newDrawable());
                msgContentFrame.setLayoutParams(msgContentFrame.getLayoutParams());
            } else {
                msgContentFrame.setBackgroundResource(com.tencent.qcloud.tim.uikit.R.drawable.chat_other_bg);
            }
        }

        //// 聊天气泡的点击事件处理
        if (onItemClickListener != null) {
            msgContentFrame.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemClickListener.onMessageLongClick(v, position, msg);
                    return true;
                }
            });
            leftUserIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                 //  onItemClickListener.onUserIconClick(view, position, msg);
                 //   UItils.showToastSafe("122");
                    Intent intent = new Intent();
                    intent.putExtra("userId",msg.getChatRecord_SendUserID());
                    intent.putExtra("type",2);
                    intent.setClass(MainActivity.mContext, FriendInfoActivity.class);
                    MainActivity.mContext.startActivity(intent);
                }
            });
            rightUserIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   // onItemClickListener.onUserIconClick(view, position, msg);
                  //  UItils.showToastSafe("123");
                }
            });
        }

     /*  msgContentFrame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null)
                onItemClickListener.onMessageLongClick(v, position, msg);
               // ChatFragment.mChatLayout.getMessageLayout().showItemPopMenu(position - 1, msg, v);
            }
        });*/

        //// 发送状态的设置
        if (msg.getStatus() == MessageInfo.MSG_STATUS_SEND_FAIL) {
            statusImage.setVisibility(View.VISIBLE);
            msgContentFrame.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onMessageLongClick(msgContentFrame, position, msg);
                    }
                }
            });
        } else {
            msgContentFrame.setOnClickListener(null);
            statusImage.setVisibility(View.GONE);
        }

        //// 左右边的消息需要调整一下内容的位置
        if (msg.isSelf()) {
            msgContentLinear.removeView(msgContentFrame);
            msgContentLinear.addView(msgContentFrame);
        } else {
            msgContentLinear.removeView(msgContentFrame);
            msgContentLinear.addView(msgContentFrame, 0);
        }
        msgContentLinear.setVisibility(View.VISIBLE);

        //// 对方已读标识的设置
       /* if (TUIKitConfigs.getConfigs().getGeneralConfig().isShowRead()) {
            if (msg.isSelf()) {
                if (msg.isGroup()) {
                    isReadText.setVisibility(View.GONE);
                } else {
                    isReadText.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) isReadText.getLayoutParams();
                    params.gravity = Gravity.CENTER_VERTICAL;
                    isReadText.setLayoutParams(params);
                    if (msg.isPeerRead()) {
                        isReadText.setText(com.tencent.qcloud.tim.uikit.R.string.has_read);
                    } else {
                        isReadText.setText(com.tencent.qcloud.tim.uikit.R.string.unread);
                    }
                }
            } else {
                isReadText.setVisibility(View.GONE);
            }
        }*/

        //// 音频已读
        unreadAudioText.setVisibility(View.GONE);

        //// 由子类设置指定消息类型的views
        layoutVariableViews(msg, position);
    }

    public abstract void layoutVariableViews(final ChatDetailsBean msg, final int position);
}
